package qiniufile

import (
	"fmt"
	"net/http"

	"mall/service/backend/api/internal/logic/media/qiniufile"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"

	"github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func PfopCallbackHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// body, err := bytesFromRequest(r)
		// logrus.Info(fmt.Sprintf("PfopCallbackHandler: %+v", string(body)))
		// {"id":"z2.01z201cxg7wnbzpa3k00mv2nrn000mpk","pipeline":"1382448083.scentrobe_private","code":0,"desc":"The fop was completed successfully","reqid":"nO8AAAC12AmP3p0X","inputBucket":"dujiaoxiu","inputKey":"uploads/attachment/2023/07/cdc095e9f74136f993330f0885f16128.jpg","items":[{"cmd":"watermark/1/image/a29kbzovL2R1amlhb3hpdS9sb2dvLnBuZw==/dissolve/50/gravity/NorthWest/dx/5/dy/5/ws/0.2|saveas/ZHVqaWFveGl1OnVwbG9hZHMvYXR0YWNobWVudC8yMDIzLzA3L2NkYzA5NWU5Zjc0MTM2Zjk5MzMzMGYwODg1ZjE2MTI4X3dtLmpwZw==","code":0,"desc":"The fop was completed successfully","hash":"Flvy54AhIob4Fld_7z2b4JGxP0vJ","key":"uploads/attachment/2023/07/cdc095e9f74136f993330f0885f16128_wm.jpg","returnOld":0}]}
		// {"id":"z2.01z211cxgb67wmn6n100mv2nrn000hb0","pipeline":"1382448083.scentrobe_private","code":0,"desc":"The fop was completed successfully","reqid":"bcQAAADIUjLw5p0X","inputBucket":"dujiaoxiu","inputKey":"uploads/attachment/2023/07/cdc095e9f74136f993330f0885f16128.jpg","items":[{"cmd":"watermark/1/image/a29kbzovL2R1amlhb3hpdS9sb2dvLnBuZw==/dissolve/70/gravity/NorthWest/dx/5/dy/5/ws/0.2|saveas/ZHVqaWFveGl1OnVwbG9hZHMvYXR0YWNobWVudC8yMDIzLzA3L2NkYzA5NWU5Zjc0MTM2Zjk5MzMzMGYwODg1ZjE2MTI4X3dtLmpwZw==","code":0,"desc":"The fop was completed successfully","hash":"Fgp5SUJmjNW6QIeTj0Sq9VzeakFZ","key":"uploads/attachment/2023/07/cdc095e9f74136f993330f0885f16128_wm.jpg","returnOld":0}]}

		ctx := r.Context()
		var req types.PfopCallbackReq
		if err := httpx.Parse(r, &req); err != nil {
			logrus.Info(fmt.Sprintf("PfopCallbackHandler httpx.Parse err: %+v", err))
			httpx.ErrorCtx(ctx, w, err)
			return
		}

		l := qiniufile.NewPfopCallbackLogic(r, svcCtx)
		resp, err := l.PfopCallback(&req)
		if err != nil {
			logrus.Info(fmt.Sprintf("PfopCallbackHandler err: %+v", err))
			httpx.ErrorCtx(ctx, w, err)
		} else {
			logrus.Info(fmt.Sprintf("PfopCallbackHandler resp: %+v", resp))
			httpx.OkJsonCtx(ctx, w, resp)
		}
	}
}
